package com.trace.demo.entity;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 * 产品实体类
 */
@Entity
@Table(name = "products")
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "product_name", nullable = false)
    @JsonAlias({"productName", "name"})
    private String productName;

    @Column(name = "category")
    private String category;

    @Column(name = "specification")
    private String specification;

    @Column(name = "unit")
    private String unit;

    @Column(name = "product_code", nullable = false)
    private String productCode;

    @Column(name = "farmer_id", nullable = false)
    private Long farmerId;

    @Column(name = "description", length = 1000)
    private String description;

    @Column(name = "product_image")
    private String productImage;

    @Column(name = "batch_number", nullable = false)
    @JsonAlias({"batchNum"})
    private String batchNumber;

    @Column(name = "batch_id")
    @JsonAlias({"batchId"})
    private String batchId;

    @Column(name = "storage_methods")
    private String storageMethods;

    @Column(name = "planting_date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonAlias({"plantingDate"})
    private LocalDateTime plantingDate;

    @Column(name = "harvest_date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonAlias({"harvestDate"})
    private LocalDateTime harvestDate;

    @Column(name = "planting_area")
    private String plantingArea;

    @Column(name = "quality_level")
    private String qualityLevel;

    @Column(name = "fertilizer_used")
    private String fertilizerUsed;

    @Column(name = "pesticide_used")
    private String pesticideUsed;

    @Column(name = "production_standard")
    private String productionStandard;

    @Column(name = "origin")
    private String origin;

    @ManyToOne
    @JoinColumn(name = "farmer_id", insertable = false, updatable = false)
    private Farmer farmer;

    @Column(name = "created_at")
    @JsonAlias({"createdAt"})
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    @JsonAlias({"updatedAt"})
    private LocalDateTime updatedAt;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        if (this.createdAt == null) {
            this.createdAt = now;
        }
        if (this.updatedAt == null) {
            this.updatedAt = now;
        }
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedAt = LocalDateTime.now();
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public String getBatchNumber() {
        return batchNumber;
    }

    public void setBatchNumber(String batchNumber) {
        this.batchNumber = batchNumber;
    }

    public String getBatchId() {
        return batchId;
    }

    public void setBatchId(String batchId) {
        this.batchId = batchId;
    }

    public String getProductImage() {
        return productImage;
    }

    public void setProductImage(String productImage) {
        this.productImage = productImage;
    }

    public String getQualityLevel() {
        return qualityLevel;
    }

    public void setQualityLevel(String qualityLevel) {
        this.qualityLevel = qualityLevel;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public String getProductionStandard() {
        return productionStandard;
    }

    public void setProductionStandard(String productionStandard) {
        this.productionStandard = productionStandard;
    }

    public String getStorageMethods() {
        return storageMethods;
    }

    public void setStorageMethods(String storageMethods) {
        this.storageMethods = storageMethods;
    }

    public Long getFarmerId() {
        return farmerId;
    }

    public void setFarmerId(Long farmerId) {
        this.farmerId = farmerId;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }


}
